﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
namespace Xwork.SEO.WebSite.Controllers
{
    /// <summary>
    /// 在线充值
    /// </summary>
    [UserAuthorize]
    public class PayController : SEOController
    {
        //
        // GET: /Pay/

        public ActionResult Index()
        {
            //充值首页
            

            return View();
        }





        public ActionResult PayRecord(int? pageIndex,DateTime? start,DateTime? end)
        {
            IQueryable<Topup> q = db.Topups.OrderByDescending(p => p.ID).Where(p => p.UserID == User.ID && p.Handled);
            if (start.HasValue)
            {
                q = q.Where(p => p.CreateTime >= start);
            }
            if (end.HasValue)
            {
                DateTime realend =  end.Value.AddDays(1);
                q = q.Where(p => p.CreateTime < realend.Date);
            }
            IPagedList<Topup> list = q.ToPagedList(pageIndex.HasValue ? pageIndex.Value : 1, 20);
            return View(list);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult Offline()
        {
            return View();
        }


        public ActionResult GoAlipay(int amount)
        {
            //TODO 跳转到支付宝充值

            db.Topups.Add(new Topup
            {
                Amount = amount,
                CreateTime = DateTime.Now,
                Handled = true,
                Source = "在线充值",
                Remark = "支付宝",
                UseableAmount = User.UseableAmount,
                UserID = User.ID
            });

            db.SaveChanges();

            return View();
        }

        [AllowAnonymous]
        public ActionResult AlipayCallback()
        {
            //TODO 添加充值返回逻辑
            return View();
        }
        [AllowAnonymous]
        public ActionResult AlipayNotify()
        {
            //TODO 添加充值回调逻辑
            return View();
        }

    }
}
